package org.xlc.eureka.aspect;

import java.util.Map;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONObject;


/**
 * @author zhouman
 *
 */
@Aspect
@Component
public class EurekaControllerAspect {
    @Value("${inner.out:\"\"}")
    private String innerOutTmp;

    /**
     * 定义拦截规则：拦截所有 controller 里面所有被 注解的方法。 拦截器具体实现
     * 
     * @param pjp
     * @throws Throwable
     */
    @Before("execution(* org.springframework.cloud.netflix.eureka.server.EurekaController.status(..))")
    public void setProperties(JoinPoint point) throws Throwable {
        Map<String, Object> model = (Map<String, Object>) point.getArgs()[1];
        JSONObject json = parse();
        model.put("ipTransfer", json.toString());
    }
    
    private JSONObject parse() {
        JSONObject tmp = new JSONObject();
        String[] split = innerOutTmp.split(",");
        if(split.length > 0) {
            for (int i = 0; i < split.length; i++) {
                String single = split[i];
                parseSingle(single, tmp);
            }
        }
        return tmp;
    }

    private JSONObject parseSingle(String single, JSONObject json) {
        String[] split = single.split(":");
        if(split.length > 0) {
            json.put(split[0].trim(), split[1].trim());
            return json;
        }
        return null;
    }
}